
#include    <stdio.h>
#include    <time.h>
#include    <stdarg.h>

#include    "zsocket/udper.hpp"

using namespace std;
using namespace zsocket;

static  const char* now( void )
{
    static  char    szNow[ 64 ];
    
    time_t  t = time(NULL);
    struct tm*  ptm = localtime( &t );
    
    strftime( szNow, sizeof(szNow) - 1, "%Y-%m-%d %H:%M:%S", ptm );
    
    return  szNow;
}

static  void    debug( const char* pszFormat, ... )
{
    va_list args;
    
    va_start( args, pszFormat );
    fprintf( stderr, "%s ", now() );
    vfprintf( stderr, pszFormat, args );
    fprintf( stderr, "\n" );
    va_end( args ); 
}

int     main ( int argc, char* argv[] )
{
    int     iPort = 4344;
    int     iMaxTimes = 10000000;
    
    Udper   udper;
    
    udper.open(iPort);
    
    
    for( int i=0; i<iMaxTimes; i++ ) {
        string  data;
        Address address;
        
        try {
            udper.recvfrom( address, data, 10 );
        }
        catch( Exception& ex ) {
            debug( "fail to recv : [%d] %s", ex.getCode(), ex.what() );
            --i;
            continue;
        }

        try {
            udper.sendto( address, data );
        }
        catch( Exception& ex ) {
            debug( "fail to send : [%d] %s", ex.getCode(), ex.what() );
            --i;
            continue;
        }
    }
    
    udper.close();
    
    return  0;
}

